# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.exceptions import ValidationError
from odoo.exceptions import UserError


class Press(models.Model):
    _name = 'ebook.user'
    _description = "图书馆销个人清单"

    name = fields.Char(
        '用户名称',
        default='用户名称'
    )

    title = fields.Many2one(
        'res.groups',
        domain=[('category_id.name','=','ebook管理权限')],
        string='权限'
    )

    data = fields.Date('销售时间', required=True, default=fields.Date.today)
    address = fields.Char(string="收货地址")
    tel = fields.Char(string="电话")

    goods_ids = fields.One2many(
        'ebook.book',
        'sale_id',
        string='商品清单',
        required=True,
        ondelete='set null'
    )

    sum_money = fields.Float(
        '金额',
        readonly=True,
        compute='_compute_amount_total'
    )

    @api.one
    @api.depends('goods_ids.allprice')
    def _compute_amount_total(self):
        for line in self.goods_ids:
            self.sum_money += line.allprice
